package com.rytx.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.Subscribe;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.SubscribeParam;
import com.kuaidi100.sdk.request.SubscribeParameters;
import com.kuaidi100.sdk.request.SubscribeReq;
import com.rytx.common.config.KuaiDi100Config;
import com.rytx.common.utils.DateUtils;
import com.rytx.common.utils.StringUtils;
import com.rytx.system.domain.dto.SetCourier;
import com.rytx.system.domain.ysp.ArtOrder;
import com.rytx.system.mapper.ArtOrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rytx.system.mapper.ArtKd100Mapper;
import com.rytx.system.domain.ArtKd100;
import com.rytx.system.service.IArtKd100Service;

import javax.annotation.Resource;

/**
 * 快递100Service业务层处理
 * 
 * @author rytx
 * @date 2024-05-26
 */
@Service
public class ArtKd100ServiceImpl implements IArtKd100Service 
{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ArtKd100Mapper artKd100Mapper;
    @Resource
    private Gson gson;
    @Resource
    private KuaiDi100Config kd100Config;
    @Resource
    private ArtOrderMapper artOrderMapper;

    @Override
    public void subscribe(SetCourier setCourier) {
        try {
            logger.info("快递100订阅参数|{}|{}|{}",setCourier.getKdCompany(),setCourier.getKdNumber(),setCourier.getPhone());
            if("shunfeng".equals(setCourier.getKdCompany())||"shunfengkuaiyun".equals(setCourier.getKdCompany())){
                if (StringUtils.isBlank(setCourier.getPhone())) {
                    logger.info("顺丰快递必须填写手机号码|{}", JSONObject.toJSONString(setCourier));
                    return ;
                }
            }
            SubscribeParam subscribeParam = getSubscribeParam(setCourier);

            SubscribeReq subscribeReq = new SubscribeReq();
            subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
            subscribeReq.setParam(gson.toJson(subscribeParam));

            IBaseClient subscribe = new Subscribe();
            HttpResult execute = subscribe.execute(subscribeReq);
            execute.getBody();
            logger.info("快递100订阅结果|{}",execute.getBody());

        } catch (Exception e) {
            logger.error("快递100订阅失败",e);
            logger.info("快递100订阅失败|{}", JSONObject.toJSONString(setCourier));
        }
    }
    private SubscribeParam getSubscribeParam(SetCourier setCourier) {
        SubscribeParameters subscribeParameters = new SubscribeParameters();
        subscribeParameters.setCallbackurl(kd100Config.getNotifyUrl());
        subscribeParameters.setSalt(kd100Config.getSalt());
        subscribeParameters.setPhone(setCourier.getPhone());//shunfeng shunfengkuaiyun  顺丰速运、顺丰快运必填

        SubscribeParam subscribeParam = new SubscribeParam();
        subscribeParam.setParameters(subscribeParameters);
        subscribeParam.setCompany(setCourier.getKdCompany());//CompanyConstant.ST sdk包自带的快递公司编码
        subscribeParam.setNumber(setCourier.getKdNumber());
        subscribeParam.setKey(kd100Config.getKey());
        return subscribeParam;
    }

    @Override
    public ArtKd100 getKd100ByNo(String nu, String com) {
        return artKd100Mapper.getKd100ByNo(nu,com);
    }

    /**
     * 查询快递100
     * 
     * @param id 快递100主键
     * @return 快递100
     */
    @Override
    public ArtKd100 selectArtKd100ById(Long id)
    {
        return artKd100Mapper.selectArtKd100ById(id);
    }

    @Override
    public ArtKd100 selectArtKd100ByOrderId(Long id) {
        return artKd100Mapper.selectArtKd100ByOrderId(id);
    }

    /**
     * 查询快递100列表
     * 
     * @param artKd100 快递100
     * @return 快递100
     */
    @Override
    public List<ArtKd100> selectArtKd100List(ArtKd100 artKd100)
    {
        return artKd100Mapper.selectArtKd100List(artKd100);
    }

    /**
     * 新增快递100
     * 
     * @param artKd100 快递100
     * @return 结果
     */
    @Override
    public int insertArtKd100(ArtKd100 artKd100)
    {
        artKd100.setCreateTime(DateUtils.getNowDate());
        return artKd100Mapper.insertArtKd100(artKd100);
    }

    /**
     * 修改快递100
     * 
     * @param artKd100 快递100
     * @return 结果
     */
    @Override
    public int updateArtKd100(ArtKd100 artKd100)
    {
        artKd100.setUpdateTime(DateUtils.getNowDate());
        return artKd100Mapper.updateArtKd100(artKd100);
    }

    /**
     * 批量删除快递100
     * 
     * @param ids 需要删除的快递100主键
     * @return 结果
     */
    @Override
    public int deleteArtKd100ByIds(Long[] ids)
    {
        return artKd100Mapper.deleteArtKd100ByIds(ids);
    }

    /**
     * 删除快递100信息
     * 
     * @param id 快递100主键
     * @return 结果
     */
    @Override
    public int deleteArtKd100ById(Long id)
    {
        return artKd100Mapper.deleteArtKd100ById(id);
    }

    @Override
    public List<ArtKd100> listByOrderId(ArtOrder artOrder) {
        artOrder = artOrderMapper.selectArtOrderByParam(artOrder);
        return artKd100Mapper.selectByOrderLogisticsNo(artOrder.getLogisticsNo());
    }
}
